﻿using System;
using System.Linq;
using System.Data.Linq;
using UniMana.Data;

namespace UniMana.Data
{
    public static class Catalogue_TermData
    {
        /// <summary>
        /// Lấy học kỳ trong chương trình đào tạo
        /// </summary>
        /// <param name="nTerm"></param>
        /// <param name="nSubjectOpen"></param>
        /// <returns></returns>

        public static IQueryable GetTermInTrainingProgram(this Table<Catalogue_Term> nTerm, Table<Training_Subject_Open> nSubjectOpen)
        {
            var term = (from t in nSubjectOpen
                        join t0 in nTerm on new { Catalogue_TermKey = Convert.ToInt32(t.Catalogue_TermKey) } equals new { Catalogue_TermKey = t0.Catalogue_TermKey }
                        select new
                        {
                            Catalogue_TermKey = (System.Int32?)t.Catalogue_TermKey,
                            t0.Name
                        }).Distinct();
            return term.Distinct();
        }

        /// <summary>
        /// Lấy học kỳ với khoa, bậc, ngành, loại hình, khóa học và năm học
        /// </summary>
        /// <param name="nTerm"></param>
        /// <param name="nSubjectOpen"></param>
        /// <param name="m_iLevel"></param>
        /// <param name="m_iMajor"></param>
        /// <param name="m_iTrainingType"></param>
        /// <param name="m_iCourse"></param>
        /// <param name="m_iSchoolYear"></param>
        /// <returns></returns>

        public static IQueryable GetTermByLevelMajorTrainingTypeCourseSchoolYear(this Table<Catalogue_Term> nTerm, Table<Training_Subject_Open> nSubjectOpen, int m_iLevelKey, int m_iMajorKey, int m_iTrainingTypeKey, int m_iCourseKey, int m_iSchoolYearKey)
        {
            var term = (from t in nSubjectOpen
                        join t0 in nTerm on new { Catalogue_TermKey = Convert.ToInt32(t.Catalogue_TermKey) } equals new { Catalogue_TermKey = t0.Catalogue_TermKey }
                        where
                          t.Catalogue_LevelKey == m_iLevelKey &&
                          t.Catalogue_MajorKey == m_iMajorKey &&
                          t.Catalogue_TrainingTypeKey == m_iTrainingTypeKey &&
                          t.Catalogue_CourseKey == m_iCourseKey &&
                          t.Catalogue_SchoolYearKey == m_iSchoolYearKey
                        select new
                        {
                            Catalogue_TermKey = (System.Int32?)t.Catalogue_TermKey,
                            t0.Name
                        }).Distinct();
            return term.Distinct();
        }

    }
}
